# Program without graphics

from vehicleIDM import vehicle
import sys
import random
import os


class trafficwo():
    def __init__(self,FName,Ncars,Tstep,Tsim,Tracklength,maxAccel,desDecel,desVelo,accelExp,TsMean,TsStdev):


        self.Fname=FName
        # Simulation Parameters#
        self.Tsim=Tsim# seconds
        self.Tstep=Tstep #seconds/step
        self.Nsteps=int(Tsim/Tstep)#simulation steps
        self.NumCars=Ncars #cars to be simulated
        
        #crash info
        self.CarCrashPos=2700
        self.CarCrashDis=150
        self.CarCrashCar=200

        #Track Data
        #RoadWidth=1000
        self.RoadLength=Tracklength
        #RoadHeight=300

        #driver Data from 
        self.desvelo=desVelo#km/h~75mph
        self.Vo=self.desvelo#/3.6#m/s
        self.Tsmean=TsMean#desired safe time headway(seconds)
        self.TsStdev=TsStdev
        self.a=maxAccel# Max accel (m/s^2)
        self.b=desDecel# Desired Deceleration (m/s^2)
        self.d=accelExp# Acceleration exponent (unit-less)
        self.S0=7.0 #jam distance 1 (m)
        self.S1=0 #jam distance 2 (m)
        self.l=5 #Car length (m)

    def run(self):

        # Simulation Parameters#
        Tsim=self.Tsim
        Tstep=self.Tstep
        Nsteps=self.Nsteps
        NumCars=int(self.NumCars)
        RoadLength=self.RoadLength
        #crash info
        CarCrashPos=self.CarCrashPos
        CarCrashDis=self.CarCrashDis
        CarCrashCar=self.CarCrashCar
        CrashCount=0

        #Track Data
        #RoadWidth=1000
        Tracklength=self.RoadLength
        #RoadHeight=300

        Vo=self.Vo
        Ts=self.Tsmean
        a=self.a
        b=self.b
        d=self.d
        S0=self.S0
        S1=self.S1
        l=self.l
        

        #object stuff
        TrackCar=range(NumCars)#which cars Do I take data from take data from
        Traffic=range(NumCars)#Vehicle objects
        FileName= open(os.getcwd()+"\\trafficdata\\"+self.Fname+'.txt','w')#Time,Position,Speed
        sys.stdout=FileName
        #Flo=open(self.Fname+'F'+'.txt','w')

        Flow=range(60/(Tstep))
        for j in Flow:
            Flow[j]=0
        

        
        for i in Traffic: #start traffic with equal spacing
            if i in TrackCar:#  data recording
                Traffic[i]=vehicle((NumCars/RoadLength)*i,0,RoadLength,i,TrackCar='On',Speedlimit=Vo,T_Step=Tstep,T=Ts,So=S0,a=a,beta=d,b=b,S1=S1)

            else:
                Traffic[i]=vehicle((NumCars/Roadlength)*i,0,RoadLength,i,Speedlimit=Vo,T_Step=Tstep,T=Ts,So=S0,a=a,beta=d,b=b,S1=S1)

        for step in range(Nsteps):
            f=0
            fsum=0
            for k in range(NumCars):
               # Traffic[k].step(10000,1000)
               
                Traffic[k].step(Traffic[(k+1)%NumCars].position,Traffic[(k+1)%NumCars].speed)
                pos=Traffic[k].position
                f=f+Traffic[k].FlowFlag

                if pos>CarCrashPos and pos<(CarCrashPos+CarCrashDis) and k==CarCrashCar and 100>CrashCount:
                    Traffic[k].setSpeed(10.0)
                    CrashCount=CrashCount+1

            Flow[step%int(60/Tstep)]=f

            for w in Flow:
                fsum=fsum+Flow[w]
            strw=str(step*Tstep) + ','+str(fsum)+'\n'
            #Flo.write(strw)
            fsum=0
            f=0
                

        for l in range(NumCars):
            Traffic[l]=[]#clear memory
       # Flo.close()
        FileName.close()           
